<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../commons/commons.jsp"%>
<!DOCTYPE html>

<head>
    <meta charset="utf-8" />
    <title>Data Tables</title>
</head>
<style>
	.info-title{color:#fff;font-size:14px;background-color:rgba(0,155,255,.8);line-height:26px;padding:0 0 0 6px;font-weight:lighter;letter-spacing:1px}
</style>
<body>
<form action="${ctx }/branchAddress/save.do" id="submit-form" method="POST">
	<div class="page-body">
		<div class="row">
			<div class="col-xs-12 col-md-12">
				<div class="widget">
					<div class="widget-body">
                     <div class="table-toolbar">
						<div id="container" style="height:800px"></div>
						<div id="myPageTop" style="margin-top:-5px">
    							<table>
        						<tr>
        							<td class="column1">
                						<label>分部：</label>
            						</td>
            						<td class="column1">
                						<input type="text" class="form-control" style="width:280px" id="branchName" name="name"/>
            						</td>
            					</tr>
            					<tr>
            						<td class="column1">
                						<label>电话：</label>
            						</td>
            						<td class="column1">
                						<input type="text" class="form-control" style="width:280px" id="phone" name="phone"/>
            						</td>
            					</tr>
            					<tr>
            						<td class="column1">
                						<label>邮箱：</label>
            						</td>
            						<td class="column1">
                						<input type="text" class="form-control" style="width:280px" id="email" name="email"/>
            						</td>
            					</tr>
            					<tr>
            						<td class="column1">
                						<label>地址：</label>
            						</td>
            						<td class="column1">
                						<input type="text" class="form-control" style="width:280px" readonly="true" id="addressName" name="address"/>
            						</td>
            						<td class="column3">
                						<input type="hidden" class="form-control" id="lnglat" name="lnglat">
            						</td>
            						<td class="column3">
            						&nbsp;&nbsp;&nbsp;&nbsp;
                						<a class="btn btn-palegreen" href="javascript:save()">提交</a>
            						</td>
        							</tr>
    							</table>
							</div>
					</div>
					</div>
				</div>
			</div>
		</div>
	</form>
</body>
<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.0&key=052c13d08fc858c30b01709e835ebe5d"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.1&key=052c13d08fc858c30b01709e835ebe5d&plugin=AMap.Autocomplete"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.1&key=052c13d08fc858c30b01709e835ebe5d&plugin=AMap.Geocoder"></script>
<script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>

<script>
	$(document).ready(function () {
	    $('#picUpload').bootstrapValidator();
	});
	var map = new AMap.Map("container", {
        resizeEnable: true
    });
    var marker=null;
    //为地图注册click事件获取鼠标点击出的经纬度坐标
    var clickEventListener = map.on('click', function(e) {
        document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat();
        if(marker==null){
        	marker = new AMap.Marker({
            icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [e.lnglat.getLng(),e.lnglat.getLat()]
        });
        marker.setMap(map);
        }else{
        	marker.setMap(null);
            marker = null;
            marker = new AMap.Marker({
            icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [e.lnglat.getLng(),e.lnglat.getLat()]
        });
        marker.setMap(map);
        }
        regeocoder([e.lnglat.getLng(),e.lnglat.getLat()]);
    });
    var auto = new AMap.Autocomplete({
        input: "tipinput"
    });
    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
    function select(e) {
        if (e.poi && e.poi.location) {
            map.setZoom(15);
            map.setCenter(e.poi.location);
        }
    }
    function addMarker(coordinate) {
    	alert([coordinate]);
        marker = new AMap.Marker({
            icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [coordinate]
        });
        marker.setMap(map);
    }
    
    function regeocoder(lnglatXY) {  //逆地理编码
        var geocoder = new AMap.Geocoder({
            radius: 1000,
            extensions: "all"
        });        
        geocoder.getAddress(lnglatXY, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
                geocoder_CallBack(result);
            }
        });        
    }
    function geocoder_CallBack(data) {
    	$("#addressName").val(data.regeocode.formattedAddress);
    }
    
    function save() {
		if ($('#branchName').val() == "") {
			warningAlert("分部名称不允许为空");
			return;
		}else if ($('#phone').val() == "") {
			warningAlert("分部联系方式不允许为空");
			return;
		}else if ($('#addressName').val() == "") {
			warningAlert("分部地址不允许为空");
			return;
		}else if ($('#lnglat').val() == "") {
			warningAlert("分部联系方式不允许为空");
			return;
		}else {
			$("#submit-form").ajaxSubmit(function(result) {
				if (result.status == 1) {
					infoAlert("添加成功");
					window.location.href = '${ctx}' + "/branchAddress/list.do";
				}else {
					warningAlert(result.message[0].msg);
				} 
			});
			return false;
		}
	}
</script>
</html>
